{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename1 =  \"/home/huangcy/data/project/scATAC-Seq_4_包lane/fastQ/FKDL190764798-1a_L6_1.fq\"\n",
    "filename2 = \"/home/huangcy/data/project/scATAC-Seq_4_包lane/fastQ/FKDL190764798-1a_L6_2.fq\"\n",
    "outdir = \"/home/huangcy/data/project/scATAC-Seq_4_包lane/fastQ/fq_out/\"\n",
    "index_file = \"/home/huangcy/data/project/scATAC-Seq_4_包lane/fastQ/index/scATAC_index_fastq_multx.csv\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==end==, len(iDict)= 1536\n"
     ]
    }
   ],
   "source": [
    "#1. get index to dict\n",
    "fr_index=open(index_file, 'r')\n",
    "\n",
    "import re\n",
    "i=0\n",
    "iDict={}\n",
    "#iDict2={}\n",
    "for lineR in fr_index.readlines():\n",
    "    i+=1\n",
    "    if i>10:\n",
    "        #break;\n",
    "        pass\n",
    "    line=lineR.strip()\n",
    "    line=re.sub('-','+',line)\n",
    "    arr=re.split(',', line)\n",
    "    iDict[arr[1]]=arr[0]\n",
    "    #if i<800:\n",
    "    #    #print(i,arr)\n",
    "    #    iDict1[arr[1]]=arr[0]\n",
    "    #else:\n",
    "    #    iDict2[arr[1]]=arr[0]\n",
    "fr_index.close()\n",
    "#print('==end==, len(iDict)=',len(iDict1),len(iDict2))\n",
    "print('==end==, len(iDict)=',len(iDict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start\n",
      "0 0.003 min\n",
      "100000000 7.273 min\n",
      "200000000 14.839 min\n",
      "300000000 22.843 min\n",
      "400000000 30.421 min\n",
      "500000000 37.779 min\n",
      "600000000 44.844 min\n",
      "700000000 53.059 min\n",
      "800000000 60.563 min\n",
      "900000000 67.779 min\n",
      "1000000000 74.863 min\n",
      "1100000000 82.128 min\n",
      "1200000000 89.84 min\n",
      "1300000000 97.313 min\n",
      "1400000000 105.335 min\n",
      "1500000000 113.315 min\n",
      "1600000000 121.734 min\n",
      "1700000000 130.146 min\n",
      "1800000000 138.59 min\n",
      "1900000000 146.962 min\n",
      "2000000000 154.814 min\n",
      "==end==, un/i= 2.49 % 50573992 2028319451\n"
     ]
    }
   ],
   "source": [
    "#2. demutiplex\n",
    "import time\n",
    "startTime=time.time()\n",
    "\n",
    "fr1=open(filename1,'r')\n",
    "fr2=open(filename2,'r')\n",
    "\n",
    "\n",
    "#open files\n",
    "outR1={}\n",
    "outR2={}\n",
    "for k,v in iDict.items(): #TAAGGCGA+ATAGAGAG sca001\n",
    "    outR1[k]=open(outdir+v+'_R1.fq','w');\n",
    "    outR2[k]=open(outdir+v+'_R2.fq','w');\n",
    "    \n",
    "#\n",
    "outR1U=open(outdir+\"undetermined\"+'_R1.fq','w');\n",
    "outR2U=open(outdir+\"undetermined\"+'_R2.fq','w');\n",
    "\n",
    "\n",
    "#open origin fq\n",
    "#linesR1=fr1.readlines();\n",
    "i=-1\n",
    "#fw_handle=''\n",
    "\n",
    "un=0 #undetermined 数量\n",
    "\n",
    "arr4R1=[]\n",
    "arr4R2=[]\n",
    "print(\"Start\")\n",
    "while True:\n",
    "    lineR1=fr1.readline()\n",
    "    lineR2=fr2.readline()\n",
    "    if not lineR1:\n",
    "        break\n",
    "    #print('read line is:', line)\n",
    "    i+=1\n",
    "    if i>200000:\n",
    "        #breakS\n",
    "        pass\n",
    "    #\n",
    "    # 进度条\n",
    "    if i %100000000==0:\n",
    "        print(i,  round((time.time()-startTime)/60, 3), 'min')\n",
    "    \n",
    "    line1=lineR1.strip()\n",
    "    #print(i,line1)\n",
    "    #\n",
    "    if i%4==0: # line1 of fq\n",
    "        arr4R1=[lineR1]\n",
    "        arr4R2=[lineR2]\n",
    "        \n",
    "        barcode=re.split(':',line1)[-1] #NCGACGTC+NTAGCCTT\n",
    "        \n",
    "        if barcode in iDict:\n",
    "            #print(i, barcode, iDict[barcode])\n",
    "            pass\n",
    "        else:\n",
    "            un+=1\n",
    "            #print(i, barcode, \"Undetermined\")\n",
    "            pass\n",
    "        \n",
    "        \n",
    "        if barcode in outR1:\n",
    "            fw_handle1=outR1[barcode]\n",
    "            fw_handle2=outR2[barcode]\n",
    "        else: \n",
    "            fw_handle1=outR1U;\n",
    "            fw_handle2=outR2U;\n",
    "    else: #line 234 of fq\n",
    "        arr4R1.append(lineR1)\n",
    "        arr4R2.append(lineR2)\n",
    "    \n",
    "    if i%4==3: #save\n",
    "        for j in range(len(arr4R1)):\n",
    "            fw_handle1.write(arr4R1[j])\n",
    "            fw_handle2.write(arr4R2[j])\n",
    "## end of while\n",
    "\n",
    "#close files\n",
    "for k,v in iDict.items():\n",
    "    outR1[k].close()\n",
    "    outR2[k].close()\n",
    "\n",
    "\n",
    "fr1.close()\n",
    "fr2.close()\n",
    "\n",
    "print('==end==, un/i=', round(un/i*100,2),'%', un, i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/data/jinwf/huangcy/software/fastq-multx'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
